package com.financing.platform.web.api;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.PublicActivitiesDTO;
import com.financing.platform.dto.PublicActivitiesPersonDTO;
import com.financing.platform.service.IPublicActivitiesService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.vo.PublicActivitiesVO;

@Controller("publicActivitiesActionApi")
@RequestMapping("/api/activities")
@Scope("prototype")
public class PublicActivitiesAction extends BaseAction {

	@Autowired
	private IPublicActivitiesService publicActivitiesServiceImpl;

	@RequestMapping("/queryActList.shtml")
	@ResponseBody
	public ResultMap queryActList(HttpServletRequest request, HttpServletResponse response,PublicActivitiesDTO publicActivitiesDTO,Pager pager) {

		try {
			publicActivitiesDTO.setPageNo(pager.getPageNo());
			publicActivitiesDTO.setPageSize(pager.getPageSize());
			int publicActivitiesListCount = publicActivitiesServiceImpl.publicActivitiesListCount(publicActivitiesDTO);
			pager.setTotalCount(publicActivitiesListCount);
			if(publicActivitiesListCount==0){
				resultMap.setAttribute("list", Collections.EMPTY_LIST);
			}else{
				List<PublicActivitiesVO> publicActivitiesList = publicActivitiesServiceImpl.publicActivitiesList(publicActivitiesDTO);
				resultMap.setAttribute("list", publicActivitiesList);
			}
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/queryPublicActivitieDetail.shtml")
	@ResponseBody
	public ResultMap queryPublicActivitieDetail(HttpServletRequest request, HttpServletResponse response, PublicActivitiesDTO publicActivitiesDTO) {

		try {
			resultMap.setAttribute("dto", publicActivitiesServiceImpl.queryPublicActivities(publicActivitiesDTO));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/addAct.shtml")
	@ResponseBody
	public ResultMap addAct(HttpServletRequest request, HttpServletResponse response, PublicActivitiesDTO publicActivitiesDTO) {

		try {
			DTOdateSet.setCreateInfo(publicActivitiesDTO, request.getSession());
			DTOdateSet.setUpdateInfo(publicActivitiesDTO, request.getSession());
			
			if(publicActivitiesDTO != null && "01".equals(publicActivitiesDTO.getActivitiesStatus())){
				publicActivitiesDTO.setActivitiesPublishTime(publicActivitiesDTO.getCreateDate());
			}
			
			resultMap.setAttribute("dto", publicActivitiesServiceImpl.addPublicActivities(publicActivitiesDTO));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/updatePublicActivities.shtml")
	@ResponseBody
	public ResultMap updateAct(HttpServletRequest request, HttpServletResponse response, PublicActivitiesDTO publicActivitiesDTO) {

		try {
			if(publicActivitiesDTO != null && "01".equals(publicActivitiesDTO.getActivitiesStatus())){
				publicActivitiesDTO.setActivitiesPublishTime(publicActivitiesDTO.getCreateDate());
			}
			DTOdateSet.setUpdateInfo(publicActivitiesDTO, request.getSession());
			resultMap.setAttribute("dto", publicActivitiesServiceImpl.updatePublicActivities(publicActivitiesDTO));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}
	@RequestMapping("/activitiesNowPerson.shtml")
	@ResponseBody
	public ResultMap activitiesNowPerson(HttpServletRequest request,
			HttpServletResponse response,PublicActivitiesPersonDTO publicActivitiesPersonDTO){
		try {
			resultMap.setAttribute("publicActivitiesPersonList", publicActivitiesServiceImpl.publicActivitiesPersonList(publicActivitiesPersonDTO));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (SQLException e) {
			log.error("query public activities person error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
}
